package com.hk.aop;

import java.math.BigDecimal;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hk.commons.ConstantOrderStatus;
import com.hk.commons.ConstantsCoupon;
import com.hk.commons.StringHelper;
import com.hk.commons.entity.order.OrderInfo;
import com.hk.commons.entity.order.OrderRouteInfo;
import com.hk.commons.entity.order.Reservation;
import com.hk.commons.entity.product.Payment;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.vo.OrderStrMsg;
import com.hk.service.IPointsHistory;
import com.hk.service.IUserExtraInfo;
import com.hk.service.kyc.ICoupon;
import com.hk.service.order.IOrderHandle;
import com.hk.service.order.IOrderInfo;
import com.hk.service.order.IOrderRouteInfo;
import com.hk.service.order.IPayment;
import com.hk.service.order.IReservation;
import com.hk.service.order.impl.OrderHandleContent;
import com.hk.service.sys.ISysUser;

@Service
@Aspect
public class UserExtraInfoAop {

	private Logger logger = LoggerFactory.getLogger(UserExtraInfoAop.class);

	@Autowired
	IUserExtraInfo extraInfoService;

	@Autowired
	IPointsHistory pointsHistoryImpl;

	@Autowired
	IPayment payService;

	@Autowired
	ISysUser userService;

	@Autowired
	IReservation reservationService;

	@Autowired
	IOrderRouteInfo orderRouteService;
	
	@Autowired
	IOrderInfo orderService;
	
	@Autowired
	ICoupon couponService;
	
	@Autowired
	private OrderHandleContent orderContent;

	@Pointcut("execution(* com.hk.service.sys.impl.SysUserImpl.add(..))")
	private void pointCutAddUser() {
	}

	@Pointcut("execution(* com.hk.service.order.impl.PaymentImpl.add(..))")
	private void pointCutTieCard() {
	}

	@Pointcut("execution(* com.hk.service.order.impl.OrderHandleContent.createOrderRoute(..))")
	private void pointCutAddOrder() {

	}

	@Pointcut("execution(* com.hk.service.order.impl.ReservationImpl.add(..))")
	private void pointCutAddBespeak() {

	}

	/**
	 * After 用户注册成功之后调用该方法, 增加1个推荐用户数
	 * 
	 * @param
	 */

	@AfterReturning(pointcut = "pointCutAddUser()", returning = "returnValue")
	public Boolean updRelationUserAmount(JoinPoint point, Object returnValue) {
		System.out.println("用户注册成功之后调用该方法");
		System.out.println("@AfterReturning：返回值为：" + returnValue);
		Long userId = (Long) returnValue;

		SysUser user = userService.findById(userId);

		if (user != null && StringHelper.isNotEmpty(user.getRecommendCode())) {
			SysUser finUser = userService
					.getByPhoneNum(user.getRecommendCode());
			if (finUser != null) {
				extraInfoService.addRelationUserAmount(userId, finUser.getId());
			}
		}
		return true;
	}

	/**
	 * After 用户绑卡后调用该方法, 增加1个推荐用户数
	 * 
	 * @param
	 */

	@AfterReturning(pointcut = "pointCutTieCard()", returning = "returnValue")
	public Boolean updTieCardAmount(JoinPoint point, Object returnValue) {
		logger.info("用户绑卡成功之后调用该方法");
		logger.info("@AfterReturning：返回值为：" + returnValue);
		Long paymentId = (Long) returnValue;

		Payment payment = payService.findById(paymentId);
		if (payment != null) {
			SysUser user = userService.findById(payment.getUserId());
			if (user != null
					&& StringHelper.isNotEmpty(user.getRecommendCode())) {
				SysUser finUser = userService.getByPhoneNum(user
						.getRecommendCode());
				if (finUser != null) {
					extraInfoService.addTieCardAmount(user.getId(),
							finUser.getId());
				}
			}
		}
		return true;
	}

	/**
	 * 如果客户成功预约，则添加相应的预约笔数
	 * 
	 * @param usedId
	 * @param amount
	 * @return
	 */
	/**
	 * After 用户绑卡后调用该方法, 增加1个推荐用户数
	 * 
	 * @param
	 */

	@AfterReturning(pointcut = "pointCutAddBespeak()", returning = "returnValue")
	public Boolean updBespeakOrderAmount(JoinPoint point, Object returnValue) {
		logger.info("用户预约成功之后调用该方法");
		logger.info("@AfterReturning：返回值为：" + returnValue);
		Long bespeakId = (Long) returnValue;
		Reservation reservation = reservationService.findById(bespeakId);

		if (reservation != null) {
			SysUser user = userService.findById(reservation.getUserId());
			if (user != null
					&& StringHelper.isNotEmpty(user.getRecommendCode())) {
				SysUser finUser = userService.getByPhoneNum(user
						.getRecommendCode());
				if (finUser != null) {
					extraInfoService.addBespeakOrderAmount(bespeakId,
							finUser.getId());
				}
			}
		}
		return true;
	}

	/**
	 * 如果客户下单，并且支付成功，根据用户的投资金额赠送家庭律师卡
	 * 
	 * @param usedId
	 * @param amount
	 * @return
	 */

	@AfterReturning(pointcut = "pointCutAddOrder()", returning = "returnValue")
	public Boolean addUserCard(JoinPoint point, Object returnValue) {
		logger.info("用户成功下单之后调用该方法");
		logger.info("@AfterReturning：返回值为：" + returnValue);
		Long orderId = (Long) returnValue;
		OrderInfo orderInfo = orderService.findById(orderId);
		
		Long userId = orderInfo.getUserId();
		
		BigDecimal totalValue  =  orderService.getTotalPayAmt(userId);
		
		//如果支付金额不小于50000,则赠送家庭律师卡
		if( !(totalValue.compareTo(new BigDecimal(50000)) == -1)  ){
			//赠送家庭律师卡
			couponService.sendCoupon(userId, Long.valueOf("374991421765724134"));
		}
		return true;
	}

}
